package com.zxbit.flash.ui.activity.mine;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zxbit.flash.R;
import com.zxbit.flash.adapter.BillAdapter;
import com.zxbit.flash.base.BaseTitleActivity;
import com.zxbit.flash.common.Constant;
import com.zxbit.flash.common.RouterMap;
import com.zxbit.flash.mvpview.OrderListMvpView;
import com.zxbit.flash.presenter.OrderListPresenter;
import com.zxbit.flash.response.BorrowingResponse;
import com.zxbit.flash.bean.OrderItemData;
import com.zxbit.flash.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的账单页面
 */
@Route(name = "我的借款", path = RouterMap.MINE_BROWING)
public class MineBillActivity extends BaseTitleActivity implements OrderListMvpView, BillAdapter.OnRepaymentClickListener {

    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.tv_no_list)
    TextView tvNoList;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    private OrderListPresenter presenter;
    private BillAdapter adapter;
    private List<OrderItemData> data = new ArrayList<>();
    private int mCurrentPage = 1;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        refreshLayout = findViewById(R.id.refresh_layout);
    }

    @Override
    protected void onUIReady() {
        presenter = new OrderListPresenter(this);
        refreshLayout.setEnableLoadmore(true); //开启加载
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(getAdapter());

        initListener();
        presenter.getOrderList(mCurrentPage);
        showLoadingView();
    }

    private void initListener() {
        // 下拉刷新
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mCurrentPage = 1;
                presenter.getOrderList(mCurrentPage);
            }
        });
        // 上拉加载
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mCurrentPage++;
                presenter.getOrderList(mCurrentPage);

            }
        });
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_mine_bill;
    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.mine_bill);
    }

    @OnClick(R.id.tv_no_list)
    public void onClick() {
        finish();
    }

    @Override
    public void onRepaymentClick(View view, String orderId, String repayMoney) {
        ARouter.getInstance().build(RouterMap.REPAY_METHOD_PAGE)
                .withString(Constant.ORDER_ID, orderId)
                .withString(Constant.ORDER_MONEY_NUM, repayMoney)
                .navigation();
    }

    private BillAdapter getAdapter() {
        if (adapter == null) {
            adapter = new BillAdapter(this);
            adapter.setClickListener(this);
        }
        return adapter;
    }

    @Override
    public void onGetDataNext(BorrowingResponse borrowingResponse) {
        dismissLoadingView();
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        if (borrowingResponse.getStatus() == Constant.HTTP_OK) {
            if (borrowingResponse.hasData()) { // 有数据
                tvNoList.setVisibility(View.GONE);
                if (borrowingResponse.getData().size() == Integer.valueOf(Constant.LIMIT)) {
                    refreshLayout.resetNoMoreData();//复原状态
                } else {
                    refreshLayout.finishLoadmoreWithNoMoreData();
                }
                if (mCurrentPage == 1) {
                    getAdapter().updateItems(borrowingResponse.getData());
                } else {
                    getAdapter().addItems(borrowingResponse.getData());
                }
            } else {
                mCurrentPage--;
                refreshLayout.finishLoadmoreWithNoMoreData();//显示全部加载完成，并不再触发加载更事件
                if (mCurrentPage == 0) {
                    getAdapter().removeAllItems();
                    tvNoList.setVisibility(View.VISIBLE);
                }
            }
        } else {
            ToastUtils.showShort(this, borrowingResponse.getMessage());
        }
    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }
}
